<template>
  <div class="infobox">
    <div class="itembox">
      <div class="jcbox">
        <div class="topbox">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="患者姓名">
              <el-input clearable v-model="formInline.patientName" placeholder="" style="width: 180px" />
            </el-form-item>
            <el-form-item label="记录人">
              <el-input clearable v-model="formInline.recorder" placeholder="" style="width: 180px" />
            </el-form-item>

            <el-form-item label="	记录时间">
              <el-input clearable v-model="formInline.recordTime" placeholder="" style="width: 180px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="getList">搜索</el-button>
              <el-button type="primary" @click="handleAdd">新增</el-button>
              <el-button type="primary" @click="xgbutton"> 修改 </el-button>
              <el-button type="primary" @click="deletebutton"> 删除 </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table :data="tableData" border style="width: 100%" max-height="700" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="examination" label="检查部位" align="center" />
        <el-table-column prop="diagnosis" label="检查情况" align="inspect" />
        <el-table-column prop="examinationOther" label="其他检查部位" align="center" />
        <el-table-column prop="recorder" label="记录人" align="center" />
        <el-table-column prop="recordTime" label="记录时间" align="center" />
        <el-table-column prop="remark" label="备注" align="center" />
        <el-table-column prop="reportUrl" label="报告URL" align="center">
          <template #default="scope">
            <el-image style="width: 100px; height: 50px" :src="scope.row.reportUrl" :preview-src-list="imgsrcList" />
          </template>
        </el-table-column>
        <el-table-column prop="address" center label="操作" align="center">
          <template #default="scope">
            <div class="czbox">
              <el-button size="small" @click="seeimg(scope.row.reportUrl)"> 查看 </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fybox">
        <el-pagination
          v-model:current-page="formInline.pageNum"
          v-model:page-size="formInline.pageSize"
          :page-sizes="[10, 50, 100, 200]"
          layout="total, sizes, prev, pager"
          :total="pagetotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog v-model="dialogVisible" title="新增检查" width="700" :before-close="handleClose">
      <el-form :model="form" label-width="auto" ref="ruleFormRef">
        <el-form-item label="检查报告 ">
          <el-upload v-model:file-list="fileListzy" class="upload-demo" multiple :limit="1" :http-request="uploadFile">
            <el-button type="primary">上传图片</el-button>
            <template #tip>
              <div class="el-upload__tip"></div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="检查部位" prop="examinationPart">
          <el-radio-group v-model="form.examinationPart" @change="ybchangetwo">
            <el-radio v-for="item in jcbwlist" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" size="large">
              {{ item.dictLabel }}
            </el-radio>
          </el-radio-group>
          <el-input v-model="form.examinationOther" placeholder="" style="width: 200px" clearable :disabled="showzz" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>

        <el-form-item label="检查情况" prop="inspectSituation">
          <el-select v-model="form.inspectSituation" placeholder="">
            <el-option v-for="item in jcqklist" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer savebox">
          <el-button @click="resetForm(ruleFormRef)">取消</el-button>
          <el-button type="primary" @click="savebutton"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { getjxxxListApi, addjxxxApi, jcxxinfoApi, deletejclbApi } from "@/api/hzmange/index.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRoute } from "vue-router";
const YZlist = JSON.parse(sessionStorage.getItem("YZlist"));
let jcbwlist = ref([]); //检查部位
let jcqklist = ref([]); //检查情况
jcbwlist.value = YZlist.examination_part;
console.log(11111111111, jcbwlist.value);
jcqklist.value = YZlist.inspect_situation;
let imgsrcList = ref([]);
const route = useRoute();
const routedata = reactive(route.query);
let datalist = ref([]);
var pagetotal = ref(0);
const formInline = reactive({
  pageNum: 1,
  pageSize: 10,
  patientName: "",
  recorder: "",
  recordTime: ""
});
var pagetotal = ref(0);
let dialogVisible = ref(false);
const ruleFormRef = ref(null);
const form = ref({
  examinationOther: "",
  remark: "",
  inspectSituation: "",
  examinationPart: ""
});
const tableData = ref([]);
const fileListzy = ref([]);
// 查看图片
let seeimg = e => {
  window.open(e, "_blank", "noopener,noreferrer");
};
//添加
let handleAdd = () => {
  fileListzy.value = [];
  form.value = {
    examinationOther: "",
    remark: "",
    inspectSituation: "",
    examinationPart: ""
  };
  dialogVisible.value = true;
};
//修改
function xgbutton() {
  if (selection.value.length == 1) {
    jcxxinfoApi({
      id: selection.value[0].id
    }).then(res => {
      dialogVisible.value = true;
      form.value = res.data;
      fileListzy.value = []; //文件回显
      let feilname = res.data.reportUrl.substring(res.data.reportUrl.lastIndexOf("/") + 1);
      fileListzy.value.push({ name: feilname, url: res.data.reportUrl }); //文件回显
    });
  } else {
    ElMessage({
      message: "请选择一条数据",
      type: "warning"
    });
  }
}
//删除
const deletebutton = e => {
  if (selection.value.length == 1) {
    ElMessageBox.confirm("确定删除?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    }).then(() => {
      deletejclbApi({
        id: selection.value[0].id
      }).then(res => {
        ElMessage({
          type: "success",
          message: `${res.msg}`
        });
        getList();
      });
    });
  } else {
    ElMessage({
      message: "请选择一条数据",
      type: "warning"
    });
  }
};
const handleClose = () => {
  dialogVisible.value = false;
};
let resetForm = formEl => {
  if (!formEl) return;
  formEl.resetFields();
  dialogVisible.value = false;
};
var fd = new FormData();
let savebutton = () => {
  form.value.patientName = routedata.name;
  form.value.patientId = routedata.id;
  console.log(form.value);

  addjxxxApi({
    adddata: form.value,
    file: fd
  }).then(() => {
    ElMessage.success("新增成功");
    handleClose();
    getList();
  });
};
let uploadFile = val => {
  fd.append("file", val.file);
};
let handleSizeChange = () => {
  getList();
};
let handleCurrentChange = () => {
  getList();
};
// 初始化数据
getList();
// 获取数据
function getList() {
  getjxxxListApi({}).then(res => {
    tableData.value = res.rows;
    pagetotal.value = res.total;
  });
}
let selection = ref([]);
const handleSelectionChange = e => {
  selection.value = e;
};
let showzz = ref(true); //.职业类型其他有输入框
let ybchangetwo = e => {
  if (e == "5") {
    showzz.value = false;
  } else {
    showzz.value = true;
    // form.value.examinationPart = "";
  }
};
</script>
<style lang="scss" scoped>
.tjjlbox {
  margin-right: 5px;
}

.itembox {
  width: 100%;
  overflow: auto;
}

.czbox {
  display: flex;
  justify-content: space-around;
}
.jcbox {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.fybox {
  display: flex;
  justify-content: end;
}
</style>
